package cn.bonoon.kernel.web.html.form;

import cn.bonoon.handler.impl.StandardAutoManager;
import cn.bonoon.kernel.annotations.Ignore;
import cn.bonoon.kernel.descriptors.ObjectDescriptor;
import cn.bonoon.kernel.descriptors.UIClassDescriptor;
import cn.bonoon.kernel.descriptors.UIPropertyAccessor;
import cn.bonoon.kernel.web.annotations.form.FormDetail;
import cn.bonoon.kernel.web.annotations.form.PropertyDetail;

/**
 * <pre>
 * 生成详细信息的界面，可以使用注解{@link cn.bonoon.kernel.web.annotations.form.FormDetail @FormDetail}；
 * 默认情况下也可以不需要指定{@link cn.bonoon.kernel.web.annotations.form.FormDetail @FormDetail}
 * 
 * 可以使用分组指定{@link cn.bonoon.kernel.web.annotations.form.FormGroup @FormGroup}
 * 
 * 可以追加帮助说明{@link cn.bonoon.kernel.web.annotations.form.FormHelper @FormHelper}
 * </pre>
 * @author jackson
 *
 */
public final class StandardDetailBuilder extends AbstractFormBuilder{

	public StandardDetailBuilder(UIClassDescriptor objectDescriptor, StandardAutoManager manager) {
		super(objectDescriptor, manager);
	}

	StandardDetailBuilder(String bindedName, ObjectDescriptor objectDescriptor, AbstractFormBuilder parent) {
		super(bindedName, objectDescriptor, parent);
	}
	
	private boolean autoIgnore;
	
	@Override
	protected void init() {
		super.init();
		FormDetail fd = uiDescriptor.getAnnotation(FormDetail.class);
		if(null != fd){
			if(fd.value() > 0){
				column= fd.value();
			}
			if(fd.headWidth() > 0){
				headWidth = fd.headWidth();
			}
			if(fd.width() > 0){
				propertyWidth = fd.width();
			}
			if(!fd.formStyleClass().isEmpty()){
				tableClass = fd.formStyleClass();
			}
			autoIgnore = fd.autoIgnore();
		}
	}
	
	@Override
	protected PropertyDetailParser readProperty(UIPropertyAccessor accessor) {
		if(null == accessor.getAnnotation(Ignore.class)){
			PropertyDetail pd = accessor.getAnnotation(PropertyDetail.class);
			if(null != pd){
				if(!pd.ignore()){
					return new PropertyDetailParser(this, accessor, pd);
				}
			}else if(!autoIgnore){
				//没有定义PropertyDetail的情况，使用默认值来生成界面
				return new PropertyDetailParser(this, accessor);
			}
		}
		return null;
	}
}
